package com.fxsh168.server.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fxsh168.server.R;
import com.fxsh168.server.base.BaseActivity;
import com.fxsh168.server.interfaces.Constant;
import com.fxsh168.server.entitys.RegiYZMCG;
import com.fxsh168.server.utils.CountDownTimerUtils;
import com.fxsh168.server.utils.MD5Utils;
import com.fxsh168.server.utils.RegExp;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import okhttp3.Call;

/**
 * Created by Administrator on 2017/3/14.
 */

public class ForgetPassActivity extends BaseActivity {
    private EditText mPutPhone;
    private EditText mPutYanzhengma;
    private TextView mGetYanzhengma;
    private EditText mPutPassword, mPutPassAgin;
    private String mPhone;
    private String mPassword;
    private String mYanzhengma;
    private List<String> mYZMS;
    private String mYZM;
    private LinearLayout mBtnRegister;
    private String mPhone2;
    Button btFindPass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fogetpass);
        mPutPhone = (EditText) findViewById(R.id.putPhone);
        mPutYanzhengma = (EditText) findViewById(R.id.putYanzhengma);
        mGetYanzhengma = (TextView) findViewById(R.id.getYanzhengma);
        mPutPassword = (EditText) findViewById(R.id.putPassword);
        btFindPass = (Button) findViewById(R.id.btFindPass);
        mPutPassAgin = (EditText) findViewById(R.id.mPutPassAgin);
        btFindPass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (canFind()) {
                    btnFindPassword();
                }
            }
        });
    }

    public void getYanzhengma(View view) {
        //获取输入框信息
        mPhone = mPutPhone.getText().toString().trim();

        if ((!TextUtils.isEmpty(mPhone)) && mPhone != null ) {
            OkHttpUtils.post()
                    .url(Constant.RETRPASSWORDYZM)
                    .addParams("phone", mPhone)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Call call, Exception e, int id) {
                            Log.e("Tag", "===onError===" + e);
                            showToast("连接不上服务器!");
                        }

                        @Override
                        public void onResponse(String response, int id) {
                            Log.e("Tag", "===onResponse===" + response);
                            if (response != null) {
                                RegiYZMCG regiYZMCG = new Gson().fromJson(response, RegiYZMCG.class);
                                Object data = regiYZMCG.getData();
                                if (data instanceof String) {
                                    String code = regiYZMCG.getCode().toString().trim();
                                    mYZM = data.toString().trim();
//                                mYZM = mYZMCG.get(0);
                                    CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(mGetYanzhengma, 60000, 1000);
                                    countDownTimerUtils.start();
                                    if (code.equals(200 + "")) {
                                        showToast("恭喜您获取验证码成功!");
                                    }
                                } else if (data instanceof List<?>) {
                                    String code = regiYZMCG.getCode().toString().trim();
                                    if (code.equals(201 + "")) {
                                        showToast("获取验证码失败!");
                                    } else if (code.equals(202 + "")) {
                                        showToast("手机号不存在!");
                                    }
                                }
                            }

                        }
                    });
        } else if ((TextUtils.isEmpty(mPhone)) || mPhone == null) {
            mPutPhone.setError("手机号不能为空!");
        } else if (mPhone.length() != 11) {
            mPutPhone.setError("请输入正确的手机号码!");
        }
    }

    //  判断是否满足条件
    public boolean canFind() {
        mYanzhengma = mPutYanzhengma.getText().toString().trim();
        mPhone2 = mPutPhone.getText().toString().trim();
        mPassword = mPutPassword.getText().toString().trim();
        String mPassAg = mPutPassAgin.getText().toString().trim();
        if (!mPhone2.equals(mPhone) && ((TextUtils.isEmpty(mPhone2))) && mPhone2 == null) {
            mPutPhone.setError("请输入11位正确的手机号!");
        } else if ((!mYZM.equals(mYanzhengma)) && (mYanzhengma == null && TextUtils.isEmpty(mYanzhengma))) {
            mPutYanzhengma.setError("验证码不正确!");
        } else if ((mPassword == null && TextUtils.isEmpty(mPassword)) && RegExp.checkPassword(mPassword) == false) {
            mPutPassword.setError("请输入6-18位字符或数字密码!");
        } else if (!mPassAg.equals(mPassword)) {
            mPutPassAgin.setError("密码不一致，请重新输入");
        } else {
            return true;
        }
        return false;
    }

    public void btnFindPassword() {
        //密码加密
        String jMPassword = MD5Utils.MD532(mPassword);
        OkHttpUtils.post()
                .addParams("phone", mPhone2).addParams("password", jMPassword)
                .url(Constant.RETRPASSWORD).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast("连接不上服务器!");
                mPutPhone.getText().clear();
                mPutYanzhengma.getText().clear();
                mPutPassword.getText().clear();
            }

            @Override
            public void onResponse(String response, int id) {
                Log.e("Tag", "==onResponse==" + response);
                RegiYZMCG regiYZMCG = new Gson().fromJson(response, RegiYZMCG.class);
                String code = regiYZMCG.getCode();
                if (code.equals(200 + "")) {
                    showToast("恭喜您，修改成功!");
                    Intent intent = new Intent(ForgetPassActivity.this, LoginActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("phone", mPhone2);
                    bundle.putString("password", mPassword);
                    intent.putExtra("retrBundle", bundle);
                    ForgetPassActivity.this.setResult(Constant.RETRRESPONDCODE, intent);
                    ForgetPassActivity.this.finish();
                } else if (code.equals(201 + "")) {
                    showToast("修改失败!");
                }
            }
        });
    }
}



